//
//  MineVC.m
//  u76ho
//
//  Created by m-air-01 on 15/12/25.
//  Copyright © 2015年 LV. All rights reserved.
//

#import "MineVC.h"
#import "MineSecCell.h"
#import "BaseInfoVC.h"
#import "MycollectionVC.h"
#import "PastExperienceVC.h"
#import "MyPictureVC.h"
#import "MyResumeVC.h"
#import "MyMessageVC.h"
#import "SetUpVC.h"
#import "LoginViewController.h"
#import "FriendImpressionViewController.h"
#import "RealNameCertifyVC.h"
@interface MineVC ()
<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>
{
    UITableView * _tableView;
    NSMutableArray * dataSource;
    NSMutableArray * picArr;
}
@end
@implementation MineVC
#pragma mark - 视图生命周期
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    [_tableView reloadData];

    
   
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
}
-(void)viewDidLoad
{
    [super viewDidLoad];
    [self main];
    self.navigationItem.title = @"我";

}
-(void)main
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginSuccess:) name:@"loginSuccess" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(modifyNameAndHeadpic:) name:@"modifyNameAndHeadpic" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(realNameNotiCase:) name:@"realNameNotiCase" object:nil];
    /**
     *  //
     */
    dataSource = [[NSMutableArray alloc]init];
    picArr = [[NSMutableArray alloc]init];
    [dataSource addObjectsFromArray:@[@[@"基本信息",@"好友印象",@"过往职场经历"],@[@"简历",@"消息",@"收藏",@"图片库"],@[@"设置"]]];
    [picArr addObjectsFromArray:@[@[@"基本信息－图标",@"好友印象－图标",@"过往经历－图标"],@[@"简历-图标",@"消息－图标",@"收藏－图标2",@"图库－图标"],@[@"设置－图标"]]];

    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-44 - 64) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView registerNib:[UINib nibWithNibName:@"MineSecCell" bundle:nil] forCellReuseIdentifier:@"MineSecCell"];
    [self.view addSubview:_tableView];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
}
#pragma mark - _tabelViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return dataSource.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[dataSource objectAtIndex:section] count];;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MineSecCell * cell = [tableView dequeueReusableCellWithIdentifier:@"MineSecCell"];
    if (indexPath.section == 0) {
        cell.contentLab.text = [dataSource[indexPath.section] objectAtIndex:indexPath.row];
        cell.headerImg.image = [UIImage imageNamed:[picArr[indexPath.section] objectAtIndex:indexPath.row]];
    }
    else if (indexPath.section == 1)
    {
        cell.contentLab.text = [dataSource[indexPath.section] objectAtIndex:indexPath.row];
        cell.headerImg.image = [UIImage imageNamed:[picArr[indexPath.section] objectAtIndex:indexPath.row]];
    }
    else
    {
        cell.contentLab.text = [dataSource[indexPath.section] objectAtIndex:indexPath.row];
        cell.headerImg.image = [UIImage imageNamed:[picArr[indexPath.section] objectAtIndex:indexPath.row]];
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self unselectCell];
    
    if (indexPath.section == 0) {
        if ([self isLogin]) {
            if (indexPath.row == 0) {
                BaseInfoVC * baseIVC = [[BaseInfoVC alloc]init];
                baseIVC.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:baseIVC animated:YES];
               // baseIVC.hidesBottomBarWhenPushed = NO;
            }
            else if (indexPath.row == 1)
            {
                FriendImpressionViewController * friendImpresstionVC = [[FriendImpressionViewController alloc]init];
                friendImpresstionVC.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:friendImpresstionVC animated:YES];
               // friendImpresstionVC.hidesBottomBarWhenPushed = NO;
            }
            else
            {
                PastExperienceVC * passExperienceVC = [[PastExperienceVC alloc]init];
                passExperienceVC.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:passExperienceVC animated:YES];
               // passExperienceVC.hidesBottomBarWhenPushed = NO;
            }
        }
        
    }
    else if (indexPath.section == 1)
    {
        if ([self isLogin]) {
            if (indexPath.row == 0) {
                MyResumeVC * myResume = [[MyResumeVC alloc]init];
                myResume.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:myResume animated:YES];
                //myResume.hidesBottomBarWhenPushed = YES;
                
            }
            else if (indexPath.row == 1)
            {
                MyMessageVC * myMessageV = [[MyMessageVC alloc]init];
                myMessageV.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:myMessageV animated:YES];
                //myMessageV.hidesBottomBarWhenPushed = NO;
            }
            else if (indexPath.row == 2)
            {
                MycollectionVC * myCollectionVC = [[MycollectionVC alloc]init];
                myCollectionVC.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:myCollectionVC animated:YES];
             //   myCollectionVC.hidesBottomBarWhenPushed = NO;
            }
            else
            {
                MyPictureVC * myPictureVC = [[MyPictureVC alloc]init];
                myPictureVC.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:myPictureVC animated:YES];
             //   myPictureVC.hidesBottomBarWhenPushed = NO;
            }

        }
        
    }
    else
    {
        if ([self isLogin]) {
            SetUpVC * setUpVC = [[SetUpVC alloc]init];
            __weak typeof(self)weakSelf = self;
            [setUpVC setLogoutBlock:^{
                [weakSelf modifyNameAndHeadpic:nil];
            }];
            setUpVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:setUpVC animated:YES];
          //  setUpVC.hidesBottomBarWhenPushed = NO;
            
        }
    }
}
-(void)unselectCell
{
    [_tableView deselectRowAtIndexPath:[_tableView indexPathForSelectedRow] animated:YES];
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return [self createHeaderView];
    }
    return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 49;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        
        if ([self isLogin]) {
            
            if (iPhone4||iPhone5) {
                return   250;
            }else if (iPhone6){
                
                return   300;
                
            }else{
                
                return 310;
                
            }
        }
        else{
            return 80;
        }
        
    }
    return 1;
}


#pragma mark - 最上面头标题
-(UIView *)createHeaderView
{
    UIImageView * headerView = [[UIImageView alloc]init];
    headerView.tag = 503;
    headerView.backgroundColor = UIColorFromRGB(0xf1f3f3);
    
    //登录状态显示
    if ([Singleton share].code) {
        //WithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SIZETO6H(271))];
        headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 0);
        
        
        
        UIImageView * topImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0)];
        topImageView.image = [UIImage imageNamed:@"a1"];
        [headerView addSubview:topImageView];
        
        UIImageView * BottomImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,0)];
        BottomImageView.image = [UIImage imageNamed:@"a2"];
        [headerView addSubview:BottomImageView];
        
        
        if (iPhone4||iPhone5) {
            headerView.height = 250;
            topImageView.height = 90;
            BottomImageView.top = topImageView.height;
            BottomImageView.height = headerView.height-90;
            
        }else if (iPhone6){
            
            headerView.height = 300;
            topImageView.height = 120;
            BottomImageView.top = topImageView.height;

            BottomImageView.height = headerView.height-120;
            
        }else{
        
            headerView.height = 310;
            topImageView.height = 125;
            BottomImageView.top = topImageView.height;

            BottomImageView.height = headerView.height-125;
        
        }
        
        
        //人物头像图片
        UIImageView *  PeopleImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, (topImageView.height-60)/2 , 60, 60)];
        
        PeopleImageView.backgroundColor = [UIColor redColor];
        

        
        NSDictionary * dic = [Singleton share].userInformation;
                    [PeopleImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",U76HOURL,[dic objectForKey:@"headpic"]]] placeholderImage:[UIImage imageNamed:@"我－头像背景"]];

        
        
        PeopleImageView.layer.masksToBounds = YES;
        PeopleImageView.layer.cornerRadius = 30;
        
        [headerView addSubview:PeopleImageView];
        
        //人名
        
        UILabel * nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, PeopleImageView.top+10, 60, 20)];
        nameLabel.text = [dic objectForKey:@"username"];
        
        nameLabel.textColor = [UIColor whiteColor];
        [headerView addSubview:nameLabel];
        
        //职位描述
        
        UILabel * ZhiWeiLabel = [[UILabel alloc]initWithFrame:CGRectMake(nameLabel.left, nameLabel.bottom+5, 200, 20)];
        ZhiWeiLabel.text = [dic objectForKey:@"duty"];
        
        ZhiWeiLabel.font = [UIFont systemFontOfSize:12];
        
        ZhiWeiLabel.textColor = [UIColor whiteColor];
        [headerView addSubview:ZhiWeiLabel];
        
        
        
        
        
        
        
        
        //非登录状态显示
    }else{
    
    headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 80);
        
        headerView.backgroundColor = [UIColor colorWithRed:0.65 green:0.80 blue:0.26 alpha:1.00];
        headerView.image = [UIImage imageNamed:@"mine_head_background3x"];
        
        
        //登录查看
        UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(30, 30, 200, 30)];
        [headerView addSubview:label];
        
        label.text = @"登录查看个人信用分详情";
        label.font = [UIFont systemFontOfSize:13];
        
        label.textColor = [UIColor whiteColor];
        
        UIButton * LoginButton = [[UIButton alloc]initWithFrame:CGRectMake( SCREEN_WIDTH - 30 -80, 30, 80, 30)];
        headerView.userInteractionEnabled = YES;
        
        [LoginButton setTitle:@"立即登录" forState:UIControlStateNormal];
        [LoginButton setTitleColor:[UIColor colorWithRed:0.65 green:0.80 blue:0.27 alpha:1.00] forState:UIControlStateNormal];
        
        LoginButton.backgroundColor = [UIColor colorWithRed:0.93 green:0.96 blue:0.85 alpha:1.00];
        
        LoginButton.layer.masksToBounds = YES;
        LoginButton.layer.cornerRadius  = 5;
        
        [LoginButton addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];
        
      
        
        [headerView addSubview:LoginButton];
    
    
    
    }
    
    
    
    
    
    return headerView;
    
}

-(void)AddLogin:(id)sender{


}
#pragma mark - 消息通知
-(void)loginSuccess:(NSNotification *)noti
{
    UIView * headerView = (UIView *)[self.view viewWithTag:503];
    for (id subV in headerView.subviews) {
        if ([subV isKindOfClass:[UIImageView class]]) {
            
        }
        else
        {
            [subV removeFromSuperview];
        }
        
        
    }
    if ([Singleton share].userInformation) {
        NSDictionary * dic = [Singleton share].userInformation;
        UIImageView * headerImg = (UIImageView *)[self.view viewWithTag:504];
        [headerImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",U76HOURL,[dic objectForKey:@"headpic"]]] placeholderImage:[UIImage imageNamed:@""]];
        
        NSString * nameStr = [dic objectForKey:@"username"];
        NSInteger length = [nameStr widthOfSize:20];
        UILabel * nameLab = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2-length/2, headerImg.bottom, length, 30)];
        nameLab.text = nameStr;
        nameLab.textAlignment = NSTextAlignmentCenter;
        [headerView addSubview:nameLab];
        
        if ([[dic objectForKey:@"isreal"] isEqualToString:@"2"]) {
            UIButton * tureNameBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            tureNameBtn.layer.masksToBounds = YES;
            tureNameBtn.layer.borderWidth = 0.5;
            tureNameBtn.layer.cornerRadius = 5;
            tureNameBtn.layer.borderColor = [[UIColor orangeColor] CGColor];
            [tureNameBtn setTitle:@"实名认证" forState:UIControlStateNormal];
            tureNameBtn.titleLabel.font = HeitiFont(10);
            [tureNameBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
            [tureNameBtn addTarget:self action:@selector(tureNameBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            tureNameBtn.frame = CGRectMake(nameLab.right+5, nameLab.top+(nameLab.height - 15)/2, 50, 15);
            [headerView addSubview:tureNameBtn];
        }

        headerImg.userInteractionEnabled = NO;

    }
}
-(void)modifyNameAndHeadpic:(NSNotification *)noti
{
    UIView * headerView = (UIView *)[self.view viewWithTag:503];
    for (id subV in headerView.subviews) {
        if ([subV isKindOfClass:[UIImageView class]]) {
            
        }
        else
        {
            [subV removeFromSuperview];
        }
        
        
    }
    if ([Singleton share].userInformation) {
        NSDictionary * dic = [Singleton share].userInformation;
        UIImageView * headerImg = (UIImageView *)[self.view viewWithTag:504];
        [headerImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",U76HOURL,[dic objectForKey:@"headpic"]]] placeholderImage:[UIImage imageNamed:@""]];
        
        NSString * nameStr = [dic objectForKey:@"username"];
        NSInteger length = [nameStr widthOfSize:20];
        UILabel * nameLab = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2-length/2, headerImg.bottom, length, 30)];
        nameLab.text = nameStr;
        nameLab.textAlignment = NSTextAlignmentCenter;
        [headerView addSubview:nameLab];
        if ([[dic objectForKey:@"isreal"] isEqualToString:@"2"]) {
            UIButton * tureNameBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            tureNameBtn.layer.masksToBounds = YES;
            tureNameBtn.layer.borderWidth = 0.5;
            tureNameBtn.layer.cornerRadius = 5;
            tureNameBtn.layer.borderColor = [[UIColor orangeColor] CGColor];
            [tureNameBtn setTitle:@"实名认证" forState:UIControlStateNormal];
            tureNameBtn.titleLabel.font = HeitiFont(10);
            [tureNameBtn addTarget:self action:@selector(tureNameBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            [tureNameBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
            tureNameBtn.frame = CGRectMake(nameLab.right+5, nameLab.top+(nameLab.height - 15)/2, 50, 15);
            [headerView addSubview:tureNameBtn];
        }
        headerImg.userInteractionEnabled = NO;
    }
    else
    {
        UIImageView * headerImg = (UIImageView *)[self.view viewWithTag:504];
        headerImg.image = nil;
        headerImg.userInteractionEnabled = YES;
        headerImg.image = [UIImage imageNamed:@"默认头像"];
    }
}
-(void)realNameNotiCase:(NSNotification *)noti
{
    if ([Singleton share].userInformation) {
        NSMutableDictionary * dic = [[NSMutableDictionary alloc]initWithDictionary:[Singleton share].userInformation];
        [dic setValue:@"1" forKey:@"isreal"];
        [Singleton share].userInformation = dic;
        [[Singleton share] saveUserInformation];
        [self modifyNameAndHeadpic:nil];
    }
}
-(void)tureNameBtnAction:(UIButton *)btn
{
    RealNameCertifyVC * realNameVC = [[RealNameCertifyVC alloc]init];
    realNameVC.isWantCertify = YES;
    realNameVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:realNameVC animated:YES];
   // realNameVC.hidesBottomBarWhenPushed = NO;
}
-(void)loginAction:(UITapGestureRecognizer *)tap
{
    
    
    
    LoginViewController * loginVC = [[LoginViewController alloc]init];
    
    UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:loginVC];
    nav.navigationBarHidden = YES;
    loginVC.isPopVC = YES;
    [self presentViewController:nav animated:YES completion:nil];
}
-(BOOL)isLogin
{
    if ([Singleton share].code) {
        NSLog(@"登录了");
        return YES;
        
    }
    else
    {
        //        NSLog(@"还没登录呢");
        //        LoginViewController * loginVC = [[LoginViewController alloc]init];
        //        [self.navigationController pushViewController:loginVC animated:NO];
        //        return NO;
        if (SYSTEM_VERSION >= 8.0) {
            UIAlertController * alerVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"您还没有登录是否登录" preferredStyle:UIAlertControllerStyleAlert];
            [alerVC addAction:[UIAlertAction actionWithTitle:@"登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
                LoginViewController * loginVC = [[LoginViewController alloc]init];
                
                UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:loginVC];
                nav.navigationBarHidden = YES;
                
                //[self.navigationController pushViewController:nav animated:YES];
                [self presentViewController:nav animated:YES completion:nil];
                
//                LoginViewController * loginVC = [[LoginViewController alloc]init];
//               
//                [self presentViewController:loginVC animated:YES completion:nil];
                
            }]];
            [alerVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                
            }]];
            [self.view.window.rootViewController presentViewController:alerVC animated:YES completion:nil];
            
        }
        else
        {
            UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您还没有登录是否登录" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alert show];
        }
        return NO;
        
    }
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    int i = (int)buttonIndex;
    if (i == 0) {
        
    }
    else if(i == 1)
    {
        LoginViewController * loginVC = [[LoginViewController alloc]init];
        
        UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:loginVC];
        nav.navigationBarHidden = YES;
        
        //[self.navigationController pushViewController:nav animated:YES];
        [self presentViewController:nav animated:YES completion:nil];
    }
}
@end
